/*   Carina
 *   Copyright (C) 2009 2010 2011  Zdravko Velinov
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _CARINA_ENTITY_INSPECTOR_HH_
#define _CARINA_ENTITY_INSPECTOR_HH_

#include <QAbstractListModel>

#include "ui_entity-inspector.h"
#include "ui_create-model-dialog.h"

#include "carina/common/ces-file.hh"
#include "carina/world-manager.hh"
#include "carina/resources.hh"

using namespace Carina;

class EntityListModel: public QAbstractListModel
{
    enum
    {
        ENTITY_NAME,
        ENTITY_COL_HIERARCHY,
        ENTITY_MESH_NAME,
        ATTR_COUNT
    };

    CESEntitySection&   m_EntSection;
    CESGeometrySection& m_GeomSection;
    WorldManagerPtr     m_WorldMgr;
    ResourceManager&    m_ResMgr;
public:
    EntityListModel(CESEntitySection& ent_section, CESGeometrySection& ces_geom, const WorldManagerPtr& wmgr, const ResourceManagerPtr& res_mgr);

    virtual int rowCount(const QModelIndex& parent=QModelIndex()) const;
    virtual int columnCount(const QModelIndex& parent=QModelIndex()) const;
    virtual QVariant data(const QModelIndex& index, int role=Qt::DisplayRole) const;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const;

    virtual bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
    virtual bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());

    bool insertRow(const string& name, const string& col, const string& mesh_name);

    bool reload();
    void clear();
};

class GeometryListModel: public QAbstractListModel
{
    enum
    {
        MESH_NAME,
        MESH_IN_PLACE,
        ATTR_COUNT
    };

    CESGeometrySection& m_GeomSection;
    ResourceManager&    m_ResMgr;
public:
    GeometryListModel(CESGeometrySection& geom_section, const ResourceManagerPtr& res_mgr);

    virtual int rowCount(const QModelIndex& parent=QModelIndex()) const;
    virtual int columnCount(const QModelIndex& parent=QModelIndex()) const;
    virtual QVariant data(const QModelIndex& index, int role=Qt::DisplayRole) const;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const;

    virtual bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
    virtual bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
    
    bool insertRow(const string& name);

    bool reload();
    void clear();
};

class CreateModelDialog: public QDialog
{
    Q_OBJECT

    Ui::CreateModelDialog   m_Dialog;
    CESFile&                m_CESFile;
public:
    CreateModelDialog(CESFile& ces_file);

    QString getName() const;
    QString getCollisionHierarchyInfo() const;
    void setMeshName(const QString& mesh_name);
    QString getMeshName() const;

    virtual void accept();
};

class EntityInspector: public QDockWidget
{
    Q_OBJECT

    Ui::EntityInspector m_EntInsp;

    EntityListModel     m_EntModel;
    GeometryListModel   m_GeomModel;
    CESFile&            m_CESFile;
    WorldManagerPtr     m_WorldMgr;
    ResourceManagerPtr  m_ResMgr;
public:
    EntityInspector(CESFile& ces_file, const WorldManagerPtr& wmgr, const ResourceManagerPtr& res_mgr);

    bool reload();
    void clear();

signals:
    void createModelInstance(const string& model_inst);

private slots:
    void on_EntityListView_customContextMenuRequested(const QPoint& pos);
    void on_GeometryListView_customContextMenuRequested(const QPoint& pos);

    void on_AddEntityButton_clicked(bool checked=false);
    void on_DeleteEntityButton_clicked(bool checked=false);
    void on_AddMeshButton_clicked(bool checked=false);
    void on_RemoveMeshButton_clicked(bool checked=false);
};

#endif /* _CARINA_ENTITY_INSPECTOR_HH_ */
